<?php

namespace app\api\controller\v41;

use app\api\controller\v13\Auth2;
use app\common\cachemodel\AppConfig;
use app\common\cachemodel\ZlcrAstr;
use app\common\cachemodel\ZlcrTag;
use app\zyhsapi\model\ZyhsConsultChatRtcRecordComment;
use app\zyhsapi\model\ZyhsFollow;
use think\response\Json;

class ConsultChat extends Auth2
{
    /**
     * 咨询师主页
     * @return Json
     * @throws \Exception
     */
    public function homePage()
    {
        $astrId = input('astr_id/d', 0);
        $zlcrAstrModel = new ZlcrAstr();
        $astrInfo = $zlcrAstrModel->getInfoById($astrId);
        if (!$astrInfo)
        {
            return show(config('code.error'), '咨询师不存在');
        }
        $res = $zlcrAstrModel->homePage($astrInfo, 1, 2);
        if ($this->user)
        {
            $resData = (new ZyhsFollow())->userFollowInfo($this->user['id'], -1);
            $followIds = array_keys($resData['data']);
            $followIds = $followIds ?: [];
        } else
        {
            $followIds = [];
        }
        $res['follow_status'] = in_array($astrId, $followIds, false) ? 1 : 2;

        $advConfig = (new AppConfig())->configInfo('zyhs');
        if ($advConfig && (int)$advConfig['adv_status'] === 1)
        {
            $res['target'] = $advConfig['adv_target'];
            $res['link'] = $advConfig['adv_link'];
            $res['btn_data'] = [
                'name' => '直接与咨询师1v1通话',
                'target' => $advConfig['adv_target'],
                'link' => $advConfig['adv_link'],
            ];
        } else
        {
            $res['btn_data'] = null;
        }
        $res['status'] = 1;
        return show(config('code.success'), '', $res);
    }

    /**
     * 占星师评价列表
     * @return Json
     * @throws \Exception
     */
    public function commentList()
    {
        $astrId = input('astr_id/d', 0);
        $list = (new ZyhsConsultChatRtcRecordComment())->astrCommentList($astrId);
        if ($list['data'])
        {
            array_multisort(array_column($list['data'], 'create_time'), SORT_NUMERIC, SORT_DESC, $list['data']);
        }
        $res = [];
        $tagsModel = new ZlcrTag();
        foreach ($list['data'] as $item)
        {
            $tagInfo = $tagsModel->batchGetInfoByIds($item['tids'], ['tag_name']);
            $res[] = [
                'name' => $item['name'],
                'avatar' => $item['avatar'],
                'time' => date('Y.m.d H:i', $item['create_time']),
                'tags' => array_column($tagInfo, 'tag_name'),
                'score' => $item['level'],
                'content' => $item['comment'],
            ];
        }
        $list['data'] = $res;
        return show(config('code.success'), 'Success.', $list);
    }
}